<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="品种名称" prop="varityName">
        <el-input v-model="queryParams.varityName" placeholder="请输入品种名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="排序条件" prop="orderBy">
        <el-select v-model="queryParams.orderBy" placeholder="请选择筛选条件" clearable>
          <el-option v-for="option in orderBy" :key="option.index" :label="option.name" :value="option.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="年份" prop="year">
        <el-select v-model="beforeSearchYear" placeholder="请选择年份" clearable>
          <el-option v-for="year in year" :key="year" :label="year" :value="year" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:varity:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:varity:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:varity:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:varity:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-info" size="mini" @click="handleChart" :disabled='!showChart'>生成柱状图
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="varityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="品种名称" align="center" prop="varityName" />
      <el-table-column v-if="(queryParams.year === null || queryParams.year == '')" label="平均年生产面积(万亩)" align="center"
        prop="comArea" />
      <el-table-column v-else :label='`${queryParams.year}年生产面积(万亩)`' align="center"
        :prop='`area${queryParams.year}`' />
      <el-table-column v-if="(queryParams.year === null || queryParams.year == '')" label="平均年产量(万吨)" align="center"
        prop="comProduction" />
      <el-table-column v-else :label='`${queryParams.year}年产量(万吨)`' align="center"
        :prop='`production${queryParams.year}`' />
      <el-table-column v-if="(queryParams.year === null || queryParams.year == '')" label="平均年成花率" align="center"
        prop="comFloweringRate" />
      <el-table-column v-else :label='`${queryParams.year}年成花率`' align="center"
        :prop='`floweringRate${queryParams.year}`' />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:varity:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:varity:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改品种对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="品种名称" prop="varityName">
          <el-input v-model="form.varityName" placeholder="请输入品种名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 柱状图弹出层 -->
    <el-dialog title="柱状图" :visible.sync="chartOpen" width="1000px" append-to-body>
      <div slot="footer" id="chart" style="width:1000px;height:600px"></div>
    </el-dialog>
  </div>
</template>
<script>
import { listVarity, getVarity, delVarity, addVarity, updateVarity } from "@/api/system/varity";
import * as echarts from 'echarts';

export default {
  name: "Varity",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 品种表格数据
      varityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示柱状图弹出层
      chartOpen: false,
      // 是否显示生成柱状图按钮
      showChart: false,
      // 年份
      beforeSearchYear: null,
      year: [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023],
      // 筛选条件
      orderBy: [
        {
          index: 0,
          name: '生产面积',
          value: 'area'
        },
        {
          index: 1,
          name: '产量',
          value: 'production'
        },
        {
          index: 2,
          name: '成花率',
          value: 'flowering_rate'
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderBy: null,
        varityName: undefined,
        year: null
      },
      // 表单参数
      form: {},
      // 柱状图参数
      chartOption: {},
      myChart: null,
      // 表单校验
      rules: {
        varityName: [
          { required: true, message: "品种名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询品种列表 */
    getList() {
      this.loading = true;
      listVarity(this.queryParams).then(response => {
        this.varityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        varityName: null,
        varityId: null,
        createTime: null,
        createBy: null,
        remark: null,
        updateTime: null,
        updateBy: null,
        comArea: null,
        comProduction: null,
        comFloweringRate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.year = this.beforeSearchYear;
      this.getList();
      if ((this.queryParams.year != null && this.queryParams.year != '') && (this.queryParams.orderBy != null && this.queryParams.orderBy != '') && (this.queryParams.varityName == null || this.queryParams.varityName == '')) {
        this.showChart = true;
      } else {
        this.showChart = false;
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.showChart = false;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.varityId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加品种";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const varityId = row.varityId || this.ids
      getVarity(varityId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改品种";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.varityId != null) {
            updateVarity(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVarity(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const varityIds = row.varityId || this.ids;
      this.$modal.confirm('是否确认删除品种编号为"' + varityIds + '"的数据项？').then(function () {
        return delVarity(varityIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/varity/export', {
        ...this.queryParams
      }, `varity_${new Date().getTime()}.xlsx`)
    },
    /** 生成柱状图操作 */
    handleChart() {
      this.chartOpen = true;
      this.chartOption = {
        tooltip: {},
        xAxis: {
          data: [this.varityList[0].varityName, this.varityList[1].varityName, this.varityList[2].varityName, this.varityList[3].varityName, this.varityList[4].varityName, this.varityList[5].varityName, this.varityList[6].varityName, this.varityList[7].varityName, this.varityList[8].varityName, this.varityList[9].varityName]
        },
        yAxis: {},
        series: [
          {
            name: '生产面积/(万亩)',
            type: 'bar',
            data: [this.varityList[0]['area' + `${this.queryParams.year}`], this.varityList[1]['area' + `${this.queryParams.year}`], this.varityList[2]['area' + `${this.queryParams.year}`], this.varityList[3]['area' + `${this.queryParams.year}`], this.varityList[4]['area' + `${this.queryParams.year}`], this.varityList[5]['area' + `${this.queryParams.year}`], this.varityList[6]['area' + `${this.queryParams.year}`], this.varityList[7]['area' + `${this.queryParams.year}`], this.varityList[8]['area' + `${this.queryParams.year}`], this.varityList[9]['area' + `${this.queryParams.year}`]]
          },
          {
            name: '产量/(万吨)',
            type: 'bar',
            data: [this.varityList[0]['production' + `${this.queryParams.year}`], this.varityList[1]['production' + `${this.queryParams.year}`], this.varityList[2]['production' + `${this.queryParams.year}`], this.varityList[3]['production' + `${this.queryParams.year}`], this.varityList[4]['production' + `${this.queryParams.year}`], this.varityList[5]['production' + `${this.queryParams.year}`], this.varityList[6]['production' + `${this.queryParams.year}`], this.varityList[7]['production' + `${this.queryParams.year}`], this.varityList[8]['production' + `${this.queryParams.year}`], this.varityList[9]['production' + `${this.queryParams.year}`]]
          },
          {
            name: '成花率/(%)',
            type: 'bar',
            data: [this.varityList[0]['floweringRate' + `${this.queryParams.year}`], this.varityList[1]['floweringRate' + `${this.queryParams.year}`], this.varityList[2]['floweringRate' + `${this.queryParams.year}`], this.varityList[3]['floweringRate' + `${this.queryParams.year}`], this.varityList[4]['floweringRate' + `${this.queryParams.year}`], this.varityList[5]['floweringRate' + `${this.queryParams.year}`], this.varityList[6]['floweringRate' + `${this.queryParams.year}`], this.varityList[7]['floweringRate' + `${this.queryParams.year}`], this.varityList[8]['floweringRate' + `${this.queryParams.year}`], this.varityList[9]['floweringRate' + `${this.queryParams.year}`]]
          }
        ],
        legend: {
          data: ['生产面积/(万亩)', '产量/(万吨)', '成花率/(%)']
        },
      }
      this.myChart = echarts.init(document.getElementById('chart'));
      this.myChart.setOption(this.chartOption);
    },
    /** 计算年平均值 */
    // insertCom() {
    //   this.varityList.forEach(varity=>{
    //     varity.comArea = ((varity.area2010+varity.area2011+varity.area2012+varity.area2013+varity.area2014+varity.area2015+varity.area2016+varity.area2017+varity.area2018+varity.area2019+varity.area2020+varity.area2021+varity.area2022)/13).toFixed(3);
    //     varity.comFloweringRate = ((varity.floweringRate2010+varity.floweringRate2011+varity.floweringRate2012+varity.floweringRate2013+varity.floweringRate2014+varity.floweringRate2015+varity.floweringRate2016+varity.floweringRate2017+varity.floweringRate2018+varity.floweringRate2019+varity.floweringRate2020+varity.floweringRate2021+varity.floweringRate2022)/13).toFixed(3);
    //     varity.comProduction = ((varity.production2010+varity.production2011+varity.production2012+varity.production2013+varity.production2014+varity.production2015+varity.production2016+varity.production2017+varity.production2018+varity.production2019+varity.production2020)/11).toFixed(3);
    //     updateVarity(varity).then(res=>{
    //       this.$modal.msgSuccess('修改成功');
    //     })
    //   });
    // }
  }
};
</script>

